package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("--------------------vo初始化 start-------------------");
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {

            if (strategyStatisVORedisService.isVoexists(strategy.getId())) {
                StrategyStatisVO vo = new StrategyStatisVO();
                //把数据库中的数据封装到vo对象中来
                BeanUtils.copyProperties(strategy,vo);
                vo.setStrategyId(strategy.getId());
                //把vo存到redis中去
                strategyStatisVORedisService.setStrategyStatisVO(vo);
            }
        }
        System.out.println("----------------------------vo结束 end----------------");
    }
}
